package models

import "github.com/astaxie/beego/orm"

type GatewayGroup struct {
	Id            int64
	Group_name    string
	Group_desc    string
	Group_user_id int
}

func (a *GatewayGroup) TableName() string {
	return TableName("gateway_group")
}

func GatewayGroupAdd(a *GatewayGroup) (int64, error) {
	return orm.NewOrm().Insert(a)
}

func GatewayGroupGetByName(gateway_groupName string) (*GatewayGroup, error) {
	a := new(GatewayGroup)
	err := orm.NewOrm().QueryTable(TableName("gateway_group")).Filter("group_name", gateway_groupName).One(a)
	if err != nil {
		return nil, err
	}
	return a, nil
}

func GatewayGroupGetList(page, pageSize int, filters ...interface{}) ([]*GatewayGroup, int64) {
	offset := (page - 1) * pageSize
	list := make([]*GatewayGroup, 0)
	query := orm.NewOrm().QueryTable(TableName("gateway_group"))
	if len(filters) > 0 {
		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	total, _ := query.Count()
	query.OrderBy("-id").Limit(pageSize, offset).All(&list)

	return list, total
}
func GatewayGroupIdName() ([]*GatewayGroup, error) {
	list := make([]*GatewayGroup, 0)
	query := orm.NewOrm().QueryTable(TableName("gateway_group"))
	_, err := query.OrderBy("-id").All(&list)

	if err != nil {
		return nil, err
	}
	return list, nil
}
func GatewayGroupIdName1(id int) ([]*GatewayGroup, error) {
	list := make([]*GatewayGroup, 0)
	query := orm.NewOrm().QueryTable(TableName("gateway_group")).Filter("group_user_id",id)
	_, err := query.OrderBy("-id").All(&list)

	if err != nil {
		return nil, err
	}
	return list, nil
}

func GetFSExternalPath(name string) (string, error) {
	var path string
	sqlStr := "SELECT config_param FROM call_base_config WHERE config_name='" + name + "'"
	 err:= orm.NewOrm().Raw(sqlStr).QueryRow(&path)

	if err != nil {
		return "", err
	}
	return path, nil
}

func GatewayGroupGetById(id int) (*GatewayGroup, error) {
	r := new(GatewayGroup)
	err := orm.NewOrm().QueryTable(TableName("gateway_group")).Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}

func (a *GatewayGroup) Update(fields ...string) error {
	if _, err := orm.NewOrm().Update(a, fields...); err != nil {
		return err
	}
	return nil
}

func (a *GatewayGroup) Delete() error {
	if _, err := orm.NewOrm().Delete(a); err != nil {
		return err
	}
	return nil
}
